package ve.com.ucv.changedetection.io;

import ve.com.ucv.changedetection.model.ConnectionManager;
import ve.com.ucv.changedetection.objects.Comparation;
import ve.com.ucv.changedetection.objects.Constants;
import ve.com.ucv.changedetection.objects.requests.ComparationRequest;
import ve.com.ucv.changedetection.objects.responses.ComparationResponse;

import java.sql.*;

/**
 * Created by josmaguirre on 1/15/15.
 */
public class ComparationIO extends ConnectionManager {
    ComparationResponse comparationResponse;
    public ComparationResponse getComparations(){
        comparationResponse = new ComparationResponse();
        Connection conn = getMySQLConnection();
        try {
            CallableStatement statement = conn.prepareCall(Constants.GET_COMPARATIONS);
            ResultSet result = statement.executeQuery();
            if (!result.next()){
                logger.info("No se han encontrado comparaciones configuradas");
                comparationResponse.setMessage("No se han encontrado comparaciones configuradas");
                comparationResponse.setMessageCode("WARN");
            }else {
                result.beforeFirst();
                while (result.next()) {
                    Comparation comparation = new Comparation();
                    comparation.setRastreo(result.getString("nombre"));
                    comparation.setFrecuencia(result.getInt("com_frecuencia"));
                    comparation.setEstado(result.getInt("com_estado"));
                    comparation.setFechaUltimoRastreo(result.getDate("com_fecha_ultimo_rastreo"));
                    comparation.setFechaProximoRastreo(result.getDate("com_fecha_proximo_rastreo"));
                    comparation.setCs(result.getDouble("com_cs"));
                    comparation.setContenido(result.getInt("com_contenido"));
                    comparation.setAtributos(result.getInt("com_atributos"));
                    comparation.setEstructura(result.getInt("com_estructura"));
                    comparationResponse.addComparation(comparation);
                }
                logger.info("Se han agregado las comparaciones encontradas");
                comparationResponse.setMessage("Comparaciones encontradas satisfactoriamente");
                comparationResponse.setMessageCode("OK");
            }

        }catch(SQLException e){
            logger.error(e);
            comparationResponse.setMessageCode("ERROR");
            comparationResponse.setMessage(e.getMessage());
        }catch(Exception e){
            logger.error(e);
            comparationResponse.setMessageCode("ERROR");
            comparationResponse.setMessage(e.getMessage());
        }
        return comparationResponse;
    }

    public ComparationResponse saveComparation(ComparationRequest comparationRequest){
        comparationResponse = new ComparationResponse();
        Connection conn = getMySQLConnection();
        try{
            CallableStatement statement = conn.prepareCall(Constants.ADD_COMPARATION);
            statement.setString(1, comparationRequest.getComparation().getRastreo());
            statement.setInt(2, comparationRequest.getComparation().getFrecuencia());
            statement.setDouble(3, comparationRequest.getComparation().getCs());
            statement.setInt(4, comparationRequest.getComparation().getContenido());
            statement.setInt(5, comparationRequest.getComparation().getAtributos());
            statement.setInt(6, comparationRequest.getComparation().getEstructura());
            statement.registerOutParameter(7, Types.VARCHAR);
            statement.registerOutParameter(8, Types.VARCHAR);
            statement.executeUpdate();

            comparationResponse.setMessage(statement.getString(8));
            comparationResponse.setMessageCode(statement.getString(7));

            return comparationResponse;
        }catch(SQLException e){
            logger.error(e);
            comparationResponse.setMessageCode("ERROR");
            comparationResponse.setMessage(e.getMessage());
        }catch(Exception e){
            logger.error(e);
            comparationResponse.setMessageCode("ERROR");
            comparationResponse.setMessage(e.getMessage());
        }
        return comparationResponse;
    }

    public ComparationResponse modifyComparation(ComparationRequest comparationRequest){
        comparationResponse = new ComparationResponse();
        Connection conn = getMySQLConnection();
        try {
            CallableStatement statement = conn.prepareCall(Constants.MODIFY_COMPARATION);
            statement.setString(1, comparationRequest.getComparation().getRastreo());
            statement.setInt(2, comparationRequest.getComparation().getFrecuencia());
            statement.setDouble(3, comparationRequest.getComparation().getCs());
            statement.setInt(4, comparationRequest.getComparation().getContenido());
            statement.setInt(5, comparationRequest.getComparation().getAtributos());
            statement.setInt(6, comparationRequest.getComparation().getEstructura());
            statement.registerOutParameter(7, Types.VARCHAR);
            statement.registerOutParameter(8, Types.VARCHAR);
            statement.executeUpdate();
            comparationResponse.setMessage(statement.getString(8));
            comparationResponse.setMessageCode(statement.getString(7));
        }catch(SQLException e){
            logger.error(e);
            comparationResponse.setMessageCode("ERROR");
            comparationResponse.setMessage(e.getMessage());
        }catch(Exception e){
            logger.error(e);
            comparationResponse.setMessageCode("ERROR");
            comparationResponse.setMessage(e.getMessage());
        }
        return comparationResponse;
    }

    public ComparationResponse deleteComparation(ComparationRequest comparationRequest){
        comparationResponse = new ComparationResponse();
        Connection conn = getMySQLConnection();
        try {
            CallableStatement statement = conn.prepareCall(Constants.DELETE_COMPARATION);
            statement.setString(1, comparationRequest.getComparation().getRastreo());
            statement.executeUpdate();
            comparationResponse.setMessageCode(statement.getString(2));
            comparationResponse.setMessage(statement.getString(3));
        }catch(SQLException e){
            logger.error(e);
            comparationResponse.setMessageCode("ERROR");
            comparationResponse.setMessage(e.getMessage());
        }catch(Exception e){
            logger.error(e);
            comparationResponse.setMessageCode("ERROR");
            comparationResponse.setMessage(e.getMessage());
        }
        return comparationResponse;
    }

    public ComparationResponse getComparationResponse() {
        return comparationResponse;
    }

    public void setComparationResponse(ComparationResponse comparationResponse) {
        this.comparationResponse = comparationResponse;
    }
}
